import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    // 用户信息
    userInfo: {
      id: 1,
      name: '管理员',
      email: 'admin@example.com',
      avatar: '👤',
      role: 'admin',
      permissions: ['read', 'write', 'delete']
    },
    // 是否已登录
    isAuthenticated: true,
    // 用户列表
    users: [
      { id: 1, name: '张三', email: 'zhangsan@example.com', role: 'admin', status: 'active', createdAt: '2024-01-15' },
      { id: 2, name: '李四', email: 'lisi@example.com', role: 'user', status: 'active', createdAt: '2024-01-20' },
      { id: 3, name: '王五', email: 'wangwu@example.com', role: 'user', status: 'inactive', createdAt: '2024-02-01' },
      { id: 4, name: '赵六', email: 'zhaoliu@example.com', role: 'moderator', status: 'active', createdAt: '2024-02-10' },
      { id: 5, name: '钱七', email: 'qianqi@example.com', role: 'user', status: 'active', createdAt: '2024-02-15' },
    ],
  }),
  // 开启持久化(安装插件piniaPluginPersistedstate并引入后才会生效)
  persist: true,

  getters: {
    // 获取用户名
    userName: (state) => state.userInfo.name,
    
    // 获取用户权限
    hasPermission: (state) => {
      return (permission) => state.userInfo.permissions.includes(permission)
    },
    
    // 获取活跃用户数
    activeUsersCount: (state) => {
      return state.users.filter(user => user.status === 'active').length
    },
    
    // 获取总用户数
    totalUsersCount: (state) => state.users.length,
    
    // 按角色筛选用户
    getUsersByRole: (state) => {
      return (role) => state.users.filter(user => user.role === role)
    }
  },

  actions: {
    // 登录
    async login(credentials) {
      try {
        // 模拟登录API调用
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        this.userInfo = {
          id: 1,
          name: '管理员',
          email: credentials.email,
          avatar: '👤',
          role: 'admin',
          permissions: ['read', 'write', 'delete']
        }
        this.isAuthenticated = true
        
        return { success: true }
      } catch (error) {
        return { success: false, error: error.message }
      }
    },
    
    // 登出
    logout() {
      this.userInfo = null
      this.isAuthenticated = false
    },
    
    // 添加用户
    addUser(userData) {
      const newUser = {
        id: Date.now(),
        ...userData,
        createdAt: new Date().toISOString().split('T')[0]
      }
      this.users.push(newUser)
      return newUser
    },
    
    // 更新用户
    updateUser(userId, updates) {
      const index = this.users.findIndex(user => user.id === userId)
      if (index !== -1) {
        this.users[index] = { ...this.users[index], ...updates }
        return this.users[index]
      }
      return null
    },
    
    // 删除用户
    deleteUser(userId) {
      const index = this.users.findIndex(user => user.id === userId)
      if (index !== -1) {
        this.users.splice(index, 1)
        return true
      }
      return false
    },
    
    // 更新用户信息
    updateUserInfo(updates) {
      this.userInfo = { ...this.userInfo, ...updates }
    }
  }
}) 